/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.guorui.ejb;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

/**
 *
 * @author lenovo
 */
@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/mid_bigwork")
    ,
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class ExtractNewBook implements MessageListener {

    @PersistenceContext(unitName = "mid_bigworkPU")
    private EntityManager em;
    
    public ExtractNewBook() {
    }
    
    @Override
    @Transactional
    public void onMessage(Message message) {
        if(message instanceof ObjectMessage) {
            ObjectMessage om = (ObjectMessage)message;
            Book book;
            try {
                book = (Book)om.getObject();
                this.persist(book);
            } catch (JMSException ex) {
                Logger.getLogger(ExtractNewBook.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void persist(Object object) {
        em.persist(object);
    }
}
